package com.lcb.rabbitmq.callback;

import com.alibaba.fastjson.JSONObject;
import com.lcb.rabbitmq.entity.MsgLog;
import com.lcb.rabbitmq.mapper.MsgLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/****
 * @Author:刘长柏
 * @Date: 2021-01-27 10:45
 * @Description:消息是否路由到queue确认
 *****/
@Component
@Slf4j
public class MyReturnCallback implements RabbitTemplate.ReturnCallback {

    @Autowired
    private MsgLogMapper msgLogMapper;

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {

        log.info("消息从Exchange路由到Queue失败: exchange: {}, route: {}, replyCode: {}, replyText: {}, message: {}", exchange, routingKey, replyCode, replyText, message);

        byte[] body = message.getBody();

        JSONObject jsonObject = JSONObject.parseObject(new String(body));
        System.out.println(jsonObject);

        MsgLog msgLog = new MsgLog();
        msgLog.setMsgId(jsonObject.getString("msgId"));
        msgLog.setStatus(2);
        msgLog.setUpdateTime(new Date());
        msgLogMapper.updateByPrimaryKeySelective(msgLog);
    }
}
